
#include "list.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

struct user_list_inter
{
	unsigned int data;
	list_t node;
};

list_t *HEAD;
list_t *HEAD_a;
list_t *HEAD_b;

void show_user_list(list_t *head)
{
	list_t *tmp = head;
	struct user_list_inter *entry;
	
	do{
		entry = list_entry(tmp, struct user_list_inter, node);
		printf("data : %d\n", entry->data);
		tmp = tmp->next;
	}while(tmp != head);	
}

void show_reversed_user_list(list_t *head)
{
	list_t *tmp = head;
	struct user_list_inter *entry;
	
	do{
		entry = list_entry(tmp, struct user_list_inter, node);
		printf("data : %d\n", entry->data);
		tmp = tmp->prev;
	}while(tmp != head);
}

list_t * merge_user_list(list_t *l, list_t *n)
{
	list_t *tmp_l = l->next;
	list_t *tmp_n = n->next;
	list_t *resoult = NULL;
	struct user_list_inter *entry_l;
	struct user_list_inter *entry_n;

	list_init(resoult);

	while(tmp_l != l && tmp_n != n)
	{
		entry_l = list_entry(tmp_l, struct user_list_inter, node);
		entry_n = list_entry(tmp_n, struct user_list_inter, node);

		if(entry_l->data <= entry_n->data){
			list_add_tail(resoult, tmp_l);
			tmp_l = tmp_l->next;
		}else{
				list_add_tail(resoult, tmp_l);
				tmp_n = tmp_n->next;
			}

		if(tmp_l == l){
			while(tmp_n != n)
			{
				list_add_tail(resoult, tmp_n);
				tmp_n = tmp_n->next;
			}
		}else if(tmp_n == n){
			while(tmp_l != l)
			{
				list_add_tail(resoult, tmp_l);
				tmp_l = tmp_l->next;
			}
		}
			

	}
}

int main()
{
	unsigned int idx;
	list_t *tmp;
	struct user_list_inter *container;
	
	container = (struct user_list_inter *)malloc(sizeof(struct user_list_inter));
	if(container){
		HEAD = &(container->node);
		list_init(HEAD);
	}else{
		printf("HEAD malloc failed\n");
		exit(-1);
	}

	container = (struct user_list_inter *)malloc(sizeof(struct user_list_inter));
	if(container){
		HEAD_a = &(container->node);
		list_init(HEAD_a);
	}

	container = (struct user_list_inter *)malloc(sizeof(struct user_list_inter));
	if(container){
		HEAD_b = &(container->node);
		list_init(HEAD_b);
	}


	/* 1. insert 5 data to list */
	for(idx = 0; idx < 5; idx++)
	{
		container = (struct user_list_inter *)malloc(sizeof(struct user_list_inter));
		if(container){
			container->data = idx;
			list_add(HEAD, &(container->node));
		}else{
			printf("node malloc failed\n");
			exit(-1);
		}
	}

	for(idx = 1; idx < 6; idx = idx + 2)
	{
		container = (struct user_list_inter *)malloc(sizeof(struct user_list_inter));
		if(container){
			container->data = idx;
			list_add_tail(HEAD_a, &(container->node));
		}
	}

	for(idx = 2; idx < 5; idx = idx + 2)
	{
		container = (struct user_list_inter *)malloc(sizeof(struct user_list_inter));
		if(container){
			container->data = idx;
			list_add_tail(HEAD_b, &(container->node));
		}
	}

	/* 2. show the whole list data */
	printf("Orginal linked list a\n");
	//show_user_list(HEAD);
	show_user_list(HEAD_a);
	
	printf("Orginal linked list b\n");
	show_user_list(HEAD_b);

	/* 3. merge */
	printf("merged linked list a\n");
	tmp = merge_user_list(HEAD_a, HEAD_b);
	show_user_list(tmp);

	/* 3. reverse user_list */
	//printf("Reversed linked list\n");
	//show_reversed_user_list(HEAD);

	return 0;
}
